package ru.jexp.rcp.supply.beans.zt;

import java.util.List;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import ru.jexp.izdelie.zakaznaaspecifikacia.ZakaznaaSpecifikaciaLite;
import ru.jexp.rcp.supply.actions.IActionNewWizard;
import ru.jexp.rcp.supply.actions.wizards.ActionNewWizardZt;
import ru.jexp.rcp.supply.beans.zs.wizard.WizardPageSelectProj;
import ru.jexp.rcp.supply.editor.EditorInputBean;
import ru.jexp.rcp.supply.editor.EditorPageRequirement;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;

public class NewWizardZt extends Wizard implements WizardViewZt, INewWizard,
		IActionNewWizard {

	public static final String ID = "ru.jexp.rcp.supply.beans.zt.newwizardzt";
	private WizardPresentorZt model;
	private IWorkbenchWindow window;

	@Override
	public boolean performFinish() {
		EditorInputBean input = new EditorInputBean(
				JExpSupplyMessages.MultiNew_action_requir_title, model
						.getRequirment());
		try {
			window.getActivePage().openEditor(input, EditorPageRequirement.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public void addPages() {
		addPage(new WizardPageSelectProj(model,
				JExpSupplyMessages.Zt_wizard_selectproj_pagename,
				JExpSupplyMessages.Zt_wizard_choosezs_pagename));
		addPage(new WizardPageChooseZs(model));
	}

	@Override
	public boolean canFinish() {
		return getFinalPage().canFinish();
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		init(workbench.getActiveWorkbenchWindow());
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;
		model = new WizardPresentorZt(this);
		setWindowTitle(JExpSupplyMessages.Zt_wizard_title);
	}

	@Override
	public IAction getAction() {
		return new ActionNewWizardZt(window, SWT.NONE);
	}

	@Override
	public WizardPageSelectProj getWizardProjPage() {
		return (WizardPageSelectProj) getPage(JExpSupplyMessages.Zt_wizard_selectproj_pagename);
	}

	@Override
	public void setZs(List<ZakaznaaSpecifikaciaLite> zsList) {
		getFinalPage().setZsList(zsList);
	}

	@Override
	public void createPageControls(Composite pageContainer) {
		super.createPageControls(pageContainer);
		WizardDialog wizardContainer = (WizardDialog) getContainer();
		wizardContainer.getShell().setImage(
				getAction().getImageDescriptor().createImage());
		wizardContainer.getShell().pack();
	}

	private WizardPageChooseZs getFinalPage() {
		return (WizardPageChooseZs) getPage(JExpSupplyMessages.Zt_wizard_choosezs_pagename);
	}
}
